package system

import (
	"parking-lot-go-dingding/model"
	"time"
)

type DingtalkSystemConfigStruct struct {
	Id            int       `json:"id" gorm:"type:int(11) auto_increment; NOT NULL; primary_key"`
	CorpId        string    `json:"corp_id" gorm:"type:varchar(255); COMMENT:'企业id'"`
	SuiteTicket   string    `json:"suite_ticket" gorm:"type:varchar(255); COMMENT:'企业ticket'"`
	RandownString string    `json:"randown_string" gorm:"type:varchar(32); COMMENT:'随机数'"`
	Alias         string    `json:"alias" gorm:"type:varchar(32); COMMENT:'别名'"`
	CreatedAt     time.Time `json:"created_at" gorm:"type:datetime"`
	UpdatedAt     time.Time `json:"updated_at" gorm:"type:datetime"`
}

func (c *DingtalkSystemConfigStruct) TableName() string {
	return "dingtalk_system_config"
}

// 更新ticket
func (c *DingtalkSystemConfigStruct) UpdateTicket(corid, ticket string) error {
	db := model.Db

	if err := db.
		Table(c.TableName()).
		Where("corp_id", corid).
		Updates(map[string]interface{}{
			"suite_ticket": ticket,
			"updated_at":   time.Now(),
		}).Error; err != nil {
		return err
	}

	return nil
}
